package cn.wizzer.app.web.modules.controllers.platform.user;

import cn.wizzer.app.sys.modules.models.SysCurrency;
import cn.wizzer.app.sys.modules.models.constant.CurrencyConstant;
import cn.wizzer.app.sys.modules.services.SysCurrencyService;
import cn.wizzer.app.user.modules.models.UserBankCard;
import cn.wizzer.app.user.modules.models.enums.BankCardTypeEnum;
import cn.wizzer.app.user.modules.services.UserBankCardService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.framework.util.Result;
import cn.wizzer.framework.util.param.ParamVerify;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

/**
 * 用户银行卡管理
 */
@IocBean
@At("/platform/user/bankCard")
public class BankCardController {
    private static final Log log = Logs.get();

    @Inject
    @Reference
    private UserBankCardService bankCardService;

    @Inject
    @Reference
    private SysCurrencyService sysCurrencyService;


    @At("/")
    @Ok("beetl:/platform/user/bankCard/index.html")
    @RequiresPermissions("user.bankCard.list")
    public void index() {

    }

    /**
     * 查询银行卡列表（分页）
     *
     * @param searchKeyword String 搜索框输入的内容
     * @param searchDate    String[] 时间数组
     * @param pageNumber    int
     * @param pageSize      int
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.bankCard.list")
    public Object data(@Param("searchKeyword") String searchKeyword,
                       @Param("searchDate") String[] searchDate,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {
        return Result.success().addData(bankCardService.listPage(pageNumber, pageSize, searchKeyword, searchDate));
    }


    /**
     * 根据用户id，币种id查询用户银行卡列表
     *
     * @param userId
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.bankCard.list")
    public Object list(@ParamVerify(required = true) String userId,
                       @ParamVerify(required = true) String currencyId) {
        Cnd cnd = Cnd.where("userId", "=", userId).and("c.delFlag", "=", 0);
        SysCurrency sysCurrency = sysCurrencyService.fetch(currencyId);
        if (!CurrencyConstant.INR.equals(sysCurrency.getName())) {
            cnd.and("b.currencyId", "=", currencyId);
        }
        return Result.success().addData(bankCardService.queryVO(cnd));
    }

    /**
     * 添加银行卡
     *
     * @param bankCard
     * @return
     */
    @At("/add")
    @Ok("json")
    @RequiresPermissions("user.bankCard.add")
    @SLog(tag = "新建BankCard", msg = "Id:${args[0].id}")
    public Object add(@Param("..") UserBankCard bankCard) {
        bankCard.setOpAt(1L);
        bankCardService.insert(bankCard);
        return Result.success();
    }


    /**
     * 根据id查询银行卡信息
     *
     * @param id String
     * @return Object
     */
    @At("/edit/?")
    @Ok("json:full")
    public Object edit(@Param("id") String id) {
        return Result.success(bankCardService.fetch(id));
    }

    /**
     * 修改银行卡信息
     *
     * @param userBankCard 用户银行卡
     * @return Object
     */
    @At("/editDo")
    @Ok("json")
    @RequiresPermissions("user.bankCard.edit")
    @SLog(tag = "修改银行卡", msg = "银行卡名:${args[1].getAttribute('name')}")
    public Object editDo(@Param("..") UserBankCard userBankCard) {
        bankCardService.update(userBankCard);
        return Result.success();
    }

    /**
     * 禁用银行卡
     *
     * @param id String
     * @return Object
     */
    @At("/disable/?")
    @Ok("json")
    @RequiresPermissions("user.bankCard.edit")
    @SLog(tag = "禁用银行卡", msg = "银行卡名:${args[1].getAttribute('name')}")
    public Object disable(@Param("id") String id) {
        bankCardService.update(Chain.make("disabled", true), Cnd.where("id", "=", id));
        return Result.success();
    }


    /**
     * 根据id启用银行卡
     *
     * @param id String
     * @return Object
     */
    @At("/enable/?")
    @Ok("json")
    @RequiresPermissions("user.bankCard.edit")
    @SLog(tag = "启用银行卡", msg = "银行卡名:${args[1].getAttribute('name')}")
    public Object enable(String id) {
        UserBankCard userBankCard = bankCardService.fetch(id);
//        if (userBankCard.isDisabled()) {
//            return Result.error("该银行卡已经有存在启用状态，请确认");
//        }
        userBankCard.setDisabled(false);
        bankCardService.update(userBankCard);
        return Result.success();
    }


    /**
     * 删除银行卡
     *
     * @param id String
     * @return Object
     */
    @At("/delete/?")
    @Ok("json")
    @RequiresPermissions("user.bankCard.delete")
    public Object delete(String id) {
        bankCardService.delete(id);
        return Result.success();
    }


    /**
     * 获取银行卡类型枚举列表
     *
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.bankCard.list")
    public Object status() {
        return Result.success(BankCardTypeEnum.toList());
    }

}
